package com.jie.deletefile.DialogGestures;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.jie.deletefile.Gestures.DataUtils;
import com.jie.deletefile.R;

import java.util.List;

public class DialogTestActivity extends AppCompatActivity  {


    private GesturesDialog dialog;
    private Button but_set_dialog;
    private Button but_test_dialog;
    private Button but_reset_dialog;
    private int hadmatch;//是否验证过密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_test);
        initView();
        /**
         * 获取存储的密码
         */
        SharedPreferences sp = getSharedPreferences("dialogGesture", MODE_PRIVATE);
        String answer = sp.getString("answer", null);
        if (answer == null) {
            hadmatch = 2;//如果没有密码为空,不用验证密码
        } else {
            hadmatch = 1;
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {

                /**
                 * 设置密码时
                 */
                case 1:
                    dialog.setTv_dialog_text("请再次输入");
                    List<Integer> answer = dialog.getDialog_ges().getchoosed();
                    Integer[] integers = DataUtils.list2integer(answer);
                    dialog.getDialog_ges().setAnswer(integers);
                    String s = DataUtils.intToString(integers);
                    SharedPreferences sp = getSharedPreferences("dialogGesture", MODE_PRIVATE);
                    SharedPreferences.Editor edit = sp.edit();
                    edit.putString("answer", s);
                    edit.commit();
                    dialog.getDialog_ges().reset();
                    break;
                /**
                 * 密码设置成功
                 */
                case 2:
                    boolean match = (boolean) msg.obj;
                    if (match) {
                        Toast.makeText(DialogTestActivity.this, "手势密码设置成功", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    } else {
                        dialog.getDialog_ges().resetmTryTimes();
                    }
                    break;
                /**
                 * 验证手势密码
                 */
                case 3:
                    boolean match1 = (boolean) msg.obj;
                    if (match1) {
                        Toast.makeText(DialogTestActivity.this, "手势密码验证成功", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();

                    } else {
                        dialog.getDialog_ges().reset();
                        dialog.setTv_dialog_text("密码错误请重新输入");

                    }
                    break;
                /**
                 * 重置密码时,验证密码成功
                 */
                case 4:
                    dialog.setTv_dialog_text("请输入新密码");
                    dialog.getDialog_ges().reset();
                    dialog.getDialog_ges().resetmTryTimes();
                    break;
                /**
                 * 重置密码时,验证密码失败
                 */
                case 5:
                    dialog.setTv_dialog_text("密码不正确 ，请重新输入");
                    dialog.getDialog_ges().reset();
                    break;
                /**
                 * 重置成功
                 */
                case 6:
                    dialog.dismiss();
                    break;

            }

        }
    };

    /**
     * 点击事件
     * @param view
     */
    public void dialogclick(View view) {
        switch (view.getId()) {
            case R.id.but_set_dialog://设置密码

                dialog = new GesturesDialog(DialogTestActivity.this, new GesturesDialog.OnGesturesListener() {
                    @Override
                    public void onBlockSelected(int ID) {

                    }

                    @Override
                    public void onGestureEvent(boolean match, int trytimes) {

                        if (trytimes == 1) {
                            handler.sendEmptyMessage(1);
                        } else {
                            Message msg = handler.obtainMessage();
                            msg.obj = match;
                            msg.what = 2;
                            handler.sendMessage(msg);
                        }
                    }

                    @Override
                    public void onUnmatchedExceedBondary() {
                    }

                    @Override
                    public void onTvForgrtClickListener() {

                    }
                });
                dialog.show();

                break;
            case R.id.but_test_dialog://验证密码
                dialog = new GesturesDialog(DialogTestActivity.this, new GesturesDialog.OnGesturesListener() {
                    @Override
                    public void onBlockSelected(int ID) {

                    }

                    @Override
                    public void onGestureEvent(boolean match, int trytimes) {
                        Message msg = handler.obtainMessage();
                        msg.what = 3;
                        msg.obj = match;
                        handler.sendMessage(msg);
                    }

                    @Override
                    public void onUnmatchedExceedBondary() {
                        Toast.makeText(DialogTestActivity.this, "密码错误次数过多，请稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onTvForgrtClickListener() {

                    }
                });
                dialog.show();
                /**
                 * 判断是否设置过密码
                 */
                SharedPreferences sp = getSharedPreferences("dialogGesture", MODE_PRIVATE);
                String answer = sp.getString("answer", null);
                if (answer == null) {
                    Toast.makeText(DialogTestActivity.this, "请先设置手势密码", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                } else {
                    Integer[] integers = DataUtils.string2integer(answer);
                    dialog.getDialog_ges().setAnswer(integers);
                }
                break;
            case R.id.but_reset_dialog://重置密码
                dialog = new GesturesDialog(DialogTestActivity.this, new GesturesDialog.OnGesturesListener() {
                    @Override
                    public void onBlockSelected(int ID) {

                    }

                    @Override
                    public void onGestureEvent(boolean match, int trytimes) {
                        if (hadmatch == 1) {
                            if (match) {
                                hadmatch++;
                                handler.sendEmptyMessage(4);

                            } else {
                                handler.sendEmptyMessage(5);
                            }
                        } else {
                            if (trytimes == 1) {
                                handler.sendEmptyMessage(1);
                            } else {
                                Message msg = handler.obtainMessage();
                                msg.obj = match;
                                msg.what = 2;
                                handler.sendMessage(msg);
                            }
                        }

                    }

                    @Override
                    public void onUnmatchedExceedBondary() {
                        Toast.makeText(DialogTestActivity.this, "密码设置失败", Toast.LENGTH_SHORT).show();
                        handler.sendEmptyMessage(6);
                    }

                    @Override
                    public void onTvForgrtClickListener() {

                    }
                });
                dialog.show();
                SharedPreferences sp1 = getSharedPreferences("dialogGesture", MODE_PRIVATE);
                String answer1 = sp1.getString("answer", null);
                if (answer1 != null) {
                    dialog.getDialog_ges().setAnswer(DataUtils.string2integer(answer1));
                }

        }
    }

    private void initView() {
        but_set_dialog = (Button) findViewById(R.id.but_set_dialog);
        but_test_dialog = (Button) findViewById(R.id.but_test_dialog);
        but_reset_dialog = (Button) findViewById(R.id.but_reset_dialog);

    }

}
